package com.yxhl.km.module.utils.mission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.widget.TextView;

import com.yxhl.km.R;
import com.yxp.permission.util.lib.PermissionUtil;
import com.yxp.permission.util.lib.callback.PermissionResultCallBack;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 张亚楠 on 2018/4/16.
 */

public class PerMissionUtils {
    private OnPerMissionResult onPerMissionResult;
    private Activity mContext;
    private TextView tv_text;
    private static PerMissionUtils perMissionUtils;

    public static PerMissionUtils getInstance() {

        if (perMissionUtils == null) {
            perMissionUtils = new PerMissionUtils();
        }

        return perMissionUtils;
    }

    public void getMission(final Activity mContext, final String[] StrPermissions, final String[] namePermission, final OnPerMissionResult onPerMissionResult) {
        this.onPerMissionResult = onPerMissionResult;
        this.mContext = mContext;
        this.tv_text = tv_text;

        PermissionUtil.getInstance().request(mContext, StrPermissions,
                new PermissionResultCallBack() {

                    List<String> listPermission = new ArrayList<>();

                    @Override
                    public void onPermissionGranted() {
                        // 当所有权限的申请被用户同意之后,该方法会被调用
                        onPerMissionResult.onPermissionGranted();
                    }

                    @Override
                    public void onPermissionGranted(String... permissions) {
                    }

                    @Override
                    public void onPermissionDenied(String... permissions) {
                        // 当权限申请中的某一个或多个权限,被用户曾经否定了,并确认了不再提醒时,也就是权限的申请窗口不能再弹出时,该方法将会被调用
                        listPermission.clear();
                        for (int i = 0; i < StrPermissions.length; i++) {
                            for (String permission : permissions) {
                                if (permission.equals(StrPermissions[i])) {
                                    listPermission.add(namePermission[i]);
                                }
                            }
                        }
                        if (permissions.length == StrPermissions.length) {
                            showDialog(listPermission);
                        }
                    }

                    @Override
                    public void onRationalShow(String... permissions) {
                        // 当权限申请中的某一个或多个权限,被用户否定了,但没有确认不再提醒时,也就是权限窗口申请时,但被否定了之后,该方法将会被调用.
                        for (int i = 0; i < StrPermissions.length; i++) {
                            for (String permission : permissions) {
                                if (permission.equals(StrPermissions[i])) {
                                    listPermission.add(namePermission[i]);
                                }
                            }
                        }
                        showDialog(listPermission);
                    }
                });

    }

    private void showDialog(List<String> str) {
        String[] array = (String[]) str.toArray(new String[str.size()]);
        AlertDialog alertDialog = new AlertDialog.Builder(mContext)
                .setTitle("开启以下权限才能使用")
                .setItems(array, null)

                .setCancelable(false)
                .setNegativeButton("不开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onPerMissionResult.onNoPermission();
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("现在开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getAppDetailSettingIntent(mContext);
                        dialog.dismiss();
                    }
                }).create();
        alertDialog.show();
        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(mContext.getResources().getColor(R.color.main_color));
        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);

//        if (mMaterialDialog == null) {
//            mMaterialDialog = new MaterialDialog(mContext);
//            mMaterialDialog.setTitle("开启以下权限才能使用哦");
//            mMaterialDialog.setMessage(str);
//            mMaterialDialog.setPositiveButton("这就去开", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    getAppDetailSettingIntent(mContext);
//                    mMaterialDialog.dismiss();
//                    mMaterialDialog = null;
//                }
//            });
//            mMaterialDialog.setNegativeButton("不开权限", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    onPerMissionResult.onNoPermission();
//                    mMaterialDialog.dismiss();
//                    mMaterialDialog = null;
//                }
//            });
//            mMaterialDialog.show();
//        }
    }


    /**
     * 跳转到权限设置界面
     */

    private void getAppDetailSettingIntent(Context context) {
        // vivo 点击设置图标>加速白名单>我的app
        //      点击软件管理>软件管理权限>软件>我的app>信任该软件
        Intent appIntent = context.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
        if (appIntent != null) {
            context.startActivity(appIntent);
            return;
        }

        // oppo 点击设置图标>应用权限管理>按应用程序管理>我的app>我信任该应用
        //      点击权限隐私>自启动管理>我的app
        appIntent = context.getPackageManager().getLaunchIntentForPackage("com.oppo.safe");
        if (appIntent != null) {
            context.startActivity(appIntent);
            return;
        }
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", mContext.getPackageName());
        }
        mContext.startActivity(intent);
    }
}
